package com.ozo.easy.common.constant;

/**
 * 枚举标准
 *
 * @author ozo
 * @date 2024-12-26
 */
public interface BaseEnum {

    String name();

    String getDesc();

    /**
     * 获取实例
     *
     * @param enumClass
     * @param name
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> E getInstance(Class<E> enumClass, String name) {
        E instance = getInstanceNoe(enumClass, name);
        if (instance == null) {
            throw new IllegalArgumentException("No enum constant " + enumClass.getCanonicalName() + " with name " + name);
        }
        return instance;
    }

    /**
     * 获取实例不抛出异常
     *
     * @param enumClass
     * @param name
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> E getInstanceNoe(Class<E> enumClass, String name) {
        for (E e : enumClass.getEnumConstants()) {
            if (e.name().equals(name)) {
                return e;
            }
        }
        return null;
    }

    /**
     * 获取实例
     *
     * @param enumClass
     * @param desc
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> E getInstanceByDesc(Class<E> enumClass, String desc) {
        E instance = getInstanceByDescNoe(enumClass, desc);
        if (instance == null) {
            throw new IllegalArgumentException("No enum constant " + enumClass.getCanonicalName() + " with desc " + desc);
        }
        return instance;
    }

    /**
     * 获取实例不抛出异常
     *
     * @param enumClass
     * @param desc
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> E getInstanceByDescNoe(Class<E> enumClass, String desc) {
        for (E e : enumClass.getEnumConstants()) {
            if (e.getDesc().equals(desc)) {
                return e;
            }
        }
        return null;
    }

    /**
     * 获取实例描述
     *
     * @param name
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> String getInstanceDesc(Class<E> clazz, String name) {
        return getInstance(clazz, name).getDesc();
    }

    /**
     * 校验name是否合法
     *
     * @param name
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> Boolean isValid(Class<E> enumClass, String name) {
        for (E e : enumClass.getEnumConstants()) {
            if (e.name().equals(name)) {
                return true;
            }
        }
        return false;
    }

}